﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TLT.Models.IRepositories;
using TLT.Models.Entities;
using TLT.Models.ShopruviDatabase;
using System.Data.Entity;

namespace TLT.Models.Repositories
{
    public class NhomSanPhamRepository : INhomSanPhamRepository,IDisposable
    {
        private ShopruviContext context;

        public NhomSanPhamRepository(ShopruviContext context)
        {
            this.context = context;
        }
        public void Add(NhomSanPham obj)
        {
            context.NhomSanPham.Add(obj);
        }
        public void Delete(Guid id)
        {
            var obj = GetObjectByID(id);
            if (obj!=null)
            {
                context.NhomSanPham.Remove(obj);
            }
        }
        public IEnumerable<NhomSanPham> GetAll()
        {
            return context.NhomSanPham.ToList();
        }
        public NhomSanPham GetObjectByID(Guid id)
        {
            return context.NhomSanPham.Find(id);
        }
        public void Update(NhomSanPham obj)
        {
            context.Entry(obj).State = EntityState.Modified;
        }
        public bool ObjectExists(Guid id)
        {
            return context.NhomSanPham.Count(e => e.Id == id) > 0;
        }
        public bool CheckDuplicateCode(Guid id, string code)
        {
            return context.NhomSanPham.Count(e => e.Ma.Equals(code) && (e.Id != id)) > 0;
        }
        // Dispose
        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
